﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;


namespace HomeWork7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
		private Graphics graphics;
		Color penColor;
		private void button1_Click(object sender, EventArgs e)
        {
			if (graphics == null)
			{
				graphics = this.CreateGraphics();
			}
			else
			{
				graphics = null;
				graphics = this.CreateGraphics();
			}
			try { drawCayleyTree(Convert.ToInt32(depth.Text), 200, 310, Convert.ToDouble(length.Text), -Math.PI / 2); }
			catch (Exception) {
				label8.Text = "无法绘制，请检查输入";
			}
        }
		void drawCayleyTree(int n,double x0, double y0, double leng, double th)
		{
			double th1 = Convert.ToDouble(th1box.Text)*Math.PI/180;
			double th2 = Convert.ToDouble(th2box.Text) * Math.PI / 180;
			double per1 = Convert.ToDouble(per111.Text);
			double per2 = Convert.ToDouble(per222.Text);
			if (n == 0) return;
			double x1 = x0 + leng * Math.Cos(th);
			double y1 = y0 + leng * Math.Sin(th);

			drawLine(x0, y0, x1, y1);

			drawCayleyTree(n - 1, x1, y1, per1 * leng, th + th1);
			drawCayleyTree(n - 1, x1, y1, per2 * leng, th - th2);
		}
		void drawLine(double x0, double y0, double x1, double y1)
		{
			Pen pen = new Pen(Color.FromArgb(penColor.ToArgb()));
			graphics.DrawLine(pen,(int)x0, (int)y0, (int)x1, (int)y1);
		}

        private void button2_Click(object sender, EventArgs e)
        {
			ColorDialog colorDialog = new ColorDialog();
			colorDialog.ShowDialog();
			penColor = colorDialog.Color;
		}
    }
}
